package com.example.interesting.idempotent.realize.aop;

import com.example.interesting.idempotent.realize.annotation.Idempotent;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.redisson.api.RedissonClient;
import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

/**
 * @Description: aop对应的advice
 * @Author: fyw
 * @History
 */
public class IdempotentMethodInterceptor implements MethodInterceptor {

    private final RedissonClient redissonClient;

    public IdempotentMethodInterceptor(RedissonClient redissonClient) {
        this.redissonClient = redissonClient;
    }

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        //防止重复执行“代理逻辑”
        Class<?> aClass = AopProxyUtils.ultimateTargetClass(invocation.getThis());
        if (!aClass.equals(invocation.getThis().getClass())) {
            return invocation.proceed();
        }
        Idempotent mergedAnnotation = AnnotatedElementUtils.findMergedAnnotation(invocation.getMethod(), Idempotent.class);
        //具体的幂等性逻辑实现
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String url = request.getRequestURI();
        Method method = invocation.getMethod();
        Idempotent annotation = invocation.getMethod().getAnnotation(Idempotent.class);


        return null;
    }

}
